package sren.serialtest

import scala.jdk.javaapi.CollectionConverters.*
import com.fazecast.jSerialComm.*
import com.sun.javafx.collections.ObservableListWrapper
import javafx.beans.property.{SimpleBooleanProperty, SimpleStringProperty}
import javafx.collections.ObservableList
import scalafx.application.JFXApp3
import scalafx.application.JFXApp3.PrimaryStage
import scalafx.collections.ObservableBuffer
import scalafx.scene.Scene
import scalafx.scene.control.{Button, ChoiceBox, TextArea, ToolBar}
import scalafx.scene.layout.BorderPane
import scalafx.stage.Stage

import scala.concurrent.ExecutionContext




@main def SerialTest(): Unit = {

  given ec: ExecutionContext = ExecutionContext.global

  val pts = SerialPort.getCommPorts()
  println(pts.map( pt => pt.getSystemPortName).toList)
  val pt = SerialPort.getCommPort("ttyUSB0")
  println( (pt.getDescriptivePortName, pt.getBaudRate, pt.getPortDescription, pt.getSystemPortName, pt.getSystemPortPath) )
  pt.setBaudRate(115200)
  pt.openPort()

  val is = pt.getInputStream()
  val os = pt.getOutputStream()

  val f = scala.concurrent.Future {
    while (true) {
      while (is.available() <= 0) {
        Thread.sleep(20)
      }
      val buf = new Array[Byte](1024)
      val readed = is.read(buf)
      println(new String(buf.take(readed)))
    }
  }

  Thread.sleep(10000)

  pt.closePort()
}

